package cn.iocoder.yudao.module.wine.controller.admin.businessuserlog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.businessuserlog.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuserlog.BusinessUserLogDO;
import cn.iocoder.yudao.module.wine.service.businessuserlog.BusinessUserLogService;

@Tag(name = "管理后台 - 商家认证日志")
@RestController
@RequestMapping("/wine/business-user-log")
@Validated
public class BusinessUserLogController {

    @Resource
    private BusinessUserLogService businessUserLogService;

    @PostMapping("/create")
    @Operation(summary = "创建商家认证日志")
    @PreAuthorize("@ss.hasPermission('wine:business-user-log:create')")
    public CommonResult<Long> createBusinessUserLog(@Valid @RequestBody BusinessUserLogSaveReqVO createReqVO) {
        return success(businessUserLogService.createBusinessUserLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商家认证日志")
    @PreAuthorize("@ss.hasPermission('wine:business-user-log:update')")
    public CommonResult<Boolean> updateBusinessUserLog(@Valid @RequestBody BusinessUserLogSaveReqVO updateReqVO) {
        businessUserLogService.updateBusinessUserLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商家认证日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:business-user-log:delete')")
    public CommonResult<Boolean> deleteBusinessUserLog(@RequestParam("id") Long id) {
        businessUserLogService.deleteBusinessUserLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商家认证日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:business-user-log:query')")
    public CommonResult<BusinessUserLogRespVO> getBusinessUserLog(@RequestParam("id") Long id) {
        BusinessUserLogDO businessUserLog = businessUserLogService.getBusinessUserLog(id);
        return success(BeanUtils.toBean(businessUserLog, BusinessUserLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商家认证日志分页")
    @PreAuthorize("@ss.hasPermission('wine:business-user-log:query')")
    public CommonResult<PageResult<BusinessUserLogRespVO>> getBusinessUserLogPage(@Valid BusinessUserLogPageReqVO pageReqVO) {
        PageResult<BusinessUserLogDO> pageResult = businessUserLogService.getBusinessUserLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessUserLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商家认证日志 Excel")
    @PreAuthorize("@ss.hasPermission('wine:business-user-log:export')")
    @OperateLog(type = EXPORT)
    public void exportBusinessUserLogExcel(@Valid BusinessUserLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessUserLogDO> list = businessUserLogService.getBusinessUserLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商家认证日志.xls", "数据", BusinessUserLogRespVO.class,
                        BeanUtils.toBean(list, BusinessUserLogRespVO.class));
    }

}